/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.FreeformViewport;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ScrollPane;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;

/**
 * @generated
 * 
 */
public class UMLPackageFigure extends CommonFigure {
	
	private static final org.eclipse.swt.graphics.Color UMLPACKAGE_COLOR = ColorConstants.blue;

	public IFigure root = null;

	protected UMLPackageHeaderFigure headerFigure;

	protected UMLPackageContentFigure contentFigure;

	public UMLPackageFigure() {
		final ToolbarLayout layout = new ToolbarLayout();
		layout.setSpacing(0);
		setLayoutManager(layout);
		
		setForegroundColor(UMLPACKAGE_COLOR);
		
		headerFigure = new UMLPackageHeaderFigure();
		add(headerFigure);
		contentFigure = new UMLPackageContentFigure();
		add(contentFigure);
		setOpaque(false);
	}

	public void changeLineColor(Color color) {
		if (color != null) {
			this.setForegroundColor(color);
			headerFigure.changeBorderColor(color);
		}
	}
	
	protected void paintShadow(Graphics g) {
		// TODO: draw the figure's own shadow here
	}

	public Label getPackageNameLabel() {
		return this.headerFigure.nameLabel;
	}

	public IFigure getContentPane() {
		return contentFigure.getContentPane();
	}
	
	@Override
	public void setBounds(Rectangle rect) {
		this.headerFigure.setBounds(rect);
		this.contentFigure.setBounds(rect);
		super.setBounds(rect);
	}
	

	class UMLPackageHeaderFigure extends CommonFigure {

		private Label nameLabel;

		public UMLPackageHeaderFigure() {
			super();

			final ToolbarLayout layout = new ToolbarLayout();
			layout.setSpacing(0);
			setLayoutManager(layout);
			setOpaque(true); // non-transparent figure
			FontData[] fontdata = { new FontData("Arial", 9, SWT.BOLD) };
			this.nameLabel = new Label("package");
			this.nameLabel.setFont(new Font(Display.getCurrent(), fontdata));
			this.nameLabel.setOpaque(false);
			this.add(this.nameLabel);
			// should look in the preferences
			setBorder(new LineBorder(UMLPackageFigure.UMLPACKAGE_COLOR));
			setForegroundColor(ColorConstants.black);
			// setBackgroundColor(ColorConstants.lightBlue);
		}

		public void setBounds(Rectangle rectangle) {
			int headerWidth = this.nameLabel.getPreferredSize().width + 8 ;
			int headerHeight = this.nameLabel.getPreferredSize().height;
			Rectangle.SINGLETON.setLocation(0, 0);
			Rectangle.SINGLETON.setSize(headerWidth, headerHeight);
//			rectangle.width = rectangle.width * 2 / 3;
			super.setBounds(Rectangle.SINGLETON);
		}
		
		public void changeBorderColor(Color color) {
			((LineBorder)getBorder()).setColor(color);
		}
	}

	class UMLPackageContentFigure extends CommonFigure {
		/** pane where the content of the element is draw */
		protected final IFigure pane;

		/**
		 * Creates a new UMLPackageContentFigure.
		 */
		public UMLPackageContentFigure() {
			super();
			// Set content layout
			 setLayoutManager(new StackLayout());

			// Add pane for content
			pane = new FreeformLayer();
			pane.setLayoutManager(new FreeformLayout());
			// Add scrollpane
			ScrollPane scrollpane = new ScrollPane();
			scrollpane.setViewport(new FreeformViewport());
			scrollpane.setContents(pane);
			
			this.add(scrollpane);
			// Set colors
			// setBackgroundColor(headerFigure.getBackgroundColor());
			setBorder(new LineBorder(1));
			setOpaque(false);
		}

		/**
		 * Returns the content pane of this figure
		 * 
		 * @return the content pane of this figure
		 */
		public IFigure getContentPane() {
			return pane;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public void setBounds(Rectangle rect) {
			int fatherHeight = getParent().getBounds().height;
			int fatherWidth  = getParent().getBounds().width;
			int headerY      = headerFigure.getSize().height;
			super.setBounds(new Rectangle(0, headerY-1, fatherWidth, fatherHeight-headerY));
		}
		
		@Override
		protected boolean useLocalCoordinates() {
			return true;
		}
		
	}
	

	@Override
	protected boolean useLocalCoordinates() {
		return true;
	}
}
